/**
 * 
 */
package com.lemote.action.account;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lemote.action.BaseAction;
import com.lemote.core.PropertyFilter;
import com.lemote.entity.Group;
import com.lemote.entity.Model;
import com.lemote.exception.AppException;
import com.lemote.service.account.ModelService;
import com.lemote.utils.CheckUtils;

/**
 * @author wuhui
 *
 * 下午02:35:23
 */
@Component("modelAction")
public class ModelAction extends BaseAction<Model, ModelService> {

	private Model entity;
	@Override
	public String delete() throws AppException {
		if (ids!=null&&ids.length()>0) {
			entityService.delete(CheckUtils.stringParse(ids));
		}
		return SUCCESS;
	}

	
	@Override
	public String input() throws AppException {
		prepareModel();
		return SUCCESS;
	}

	
	@Override
	public String list() throws AppException {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		page = entityService.search(page, filters);
		return SUCCESS;
	}

	
	@Override
	public void prepareModel() throws AppException {
		if (id != null) {
			entity = entityService.get(id);
		} else {
			entity = new Model();
		}
		
	}

	
	@Override
	public String save() throws AppException {
		String pid = request.getParameter("pid");
		if (entity!=null) {
			Model parent = null;
			if (pid!=null&&!"".equals(StringUtils.trim(pid))) {
				parent = entityService.get(Long.parseLong(pid));
				entity.setParent(parent);
			}
			if (entity.getId()==null) {
				entityService.add(entity);
			}else {
				entityService.update(entity);
			}
			return GLOBALSUCCESS;
		} 
		return ERROR;
	}
	
	/**
	 * input页面显示所有授权列表.
	 */
	public List<Model> getAllModelList() {
		
		return entityService.getAll();
	}

	@Autowired
	@Override
	public void setEntityService(ModelService entityService) {
		
		this.entityService = entityService;
	}

	public Model getEntity() {
		return entity;
	}


	public void setEntity(Model entity) {
		this.entity = entity;
	}


	public Model getModel() {
		
		return entity;
	}

}
